import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import { RootState } from "../store";


/*
意向岗位 intentPost
意向城市 intentCity
期望薪资 expectedSalary
求职类型 jobType
期望行业 expectIndustry
 */
export const jobIntentInitialState = {
    intentPost: { value: '', defaultValue: '意向岗位', display: true },
    intentCity: { value: '', defaultValue: '意向城市', display: true },
    expectedSalary: { value: '', defaultValue: '期望薪资', display: true },
    jobType: { value: '', defaultValue: '求职类型', display: true },
    expectIndustry: { value: '', defaultValue: '期望行业', display: false },
}

export type JobIntentPayload = {
    [p in keyof typeof jobIntentInitialState]: typeof jobIntentInitialState[p]['value']
}
export type DisplayMap = {
    [p in keyof typeof jobIntentInitialState]: boolean
}
export type JonIntentKeys = keyof typeof jobIntentInitialState;


const jobIntentSlice = createSlice({
    initialState: jobIntentInitialState,
    name: 'jobIntent',
    reducers: {
        reInitJobIntent: (state, action) => action.payload,
        saveJobIntent: (state, action: PayloadAction<JobIntentPayload>) => {
            for (const [key] of Object.entries(state)) {
                state[key].value = action.payload[key]
            }
        },
        changeJobIntentDisplay: (state, action: PayloadAction<DisplayMap>) => {
            for (let key in state) {
                state[key].display = action.payload[key]
            }
        }
    }
})
export const { saveJobIntent, changeJobIntentDisplay, reInitJobIntent } = jobIntentSlice.actions;

export const getJobIntentData = (state: RootState) => state.jobIntent;

export default jobIntentSlice.reducer;
